package com.idanchuang.component.xxl.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: chac
 * @PackageName: com.idanchuang.component.xxl.config
 * @ClassName: XxlJobConfiguration
 * @Description:
 * @Date: 2020/10/29 14:48
 */
@Configuration
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(XxlJobConfigBean.class)
public class XxlJobAutoConfiguration {

    @Autowired
    private XxlJobConfigBean xxlJobConfigBean;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlJobConfigBean.getAdminAddresses());
        xxlJobSpringExecutor.setAppName(xxlJobConfigBean.getAppName());
        xxlJobSpringExecutor.setIp(xxlJobConfigBean.getIp());
        if (xxlJobConfigBean.getPort() != null) {
            xxlJobSpringExecutor.setPort(xxlJobConfigBean.getPort());
        }
        xxlJobSpringExecutor.setAccessToken(xxlJobConfigBean.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlJobConfigBean.getLogPath());
        if (xxlJobConfigBean.getLogSuffix() != null && !"".equals(xxlJobConfigBean.getLogSuffix())) {
            xxlJobSpringExecutor.setLogSuffix(xxlJobConfigBean.getLogSuffix());
        }
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobConfigBean.getLogRetentionDays());
        if (xxlJobConfigBean.getEnableLog() != null) {
            xxlJobSpringExecutor.setEnableLog(xxlJobConfigBean.getEnableLog());
        }
        return xxlJobSpringExecutor;
    }

}
